#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 控件——QPushButton的属性，第一个属性——icon属性(图标)
    // 创建一个按钮，并给这个按钮设置图标
//    QPushButton* button = new QPushButton(this);
//    button->move(300, 300);
//    QIcon incon(":/mushroom.png");
//    button->setIcon(incon);
//    // 设置图标的大小
//    QSize size(50, 50);
//    button->setIconSize(size);
    ui->pushButton_target->setIcon(QIcon(":/mushroom.png"));
    ui->pushButton_target->setIconSize(QSize(100, 100));

    ui->pushButton_up->setIcon(QIcon(":/up.png"));
    ui->pushButton_up->setIconSize(QSize(70, 70));

    ui->pushButton_down->setIcon(QIcon(":/down.png"));
    ui->pushButton_down->setIconSize(QSize(70, 70));

    ui->pushButton_left->setIcon(QIcon(":/left.png"));
    ui->pushButton_left->setIconSize(QSize(70, 70));

    ui->pushButton_right->setIcon(QIcon(":/rigth.png"));
    ui->pushButton_right->setIconSize(QSize(70, 70));


    // 第二个属性shortCut(设置快捷键)

    // 方法1——直接通过按键的名字来设置，这种方式虽然简单，但是也容易出错
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 其实也可以通过按键的枚举来设置
//    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_Up));
//    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_Down));
//    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_Left));
//    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_Right));

    // 如果想要设置组合键，例如ctrl + xxx，就可以像下面这样写：
    ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));

    // 是否支持连发？
    // 键盘的按键和快捷键是默认支持连发的，但是鼠标点击默认不支持连发
    // 如果想要让鼠标点击也支持连发，就需要手动给按钮设置autoRepeat属性
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    qDebug() << "向上" << rect;
//    rect.setY(rect.y() - 5);
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    qDebug() << "向下" << rect;
//    rect.setY(rect.y() - 5);
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    qDebug() << "向左" << rect;
//    rect.setY(rect.y() - 5);
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    qDebug() << "向右" << rect;
//    rect.setY(rect.y() - 5);
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
}
